package Javaa.Base;
// create an example of serialization in net transfer in Java

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket; 
class Person implements Serializable {
    private static final long serialVersionUID = 1L;
    String name;
    int age;

    Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
}
public class SeralizeTransferExample {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        // Start server in a new thread
        new Thread(() -> {
            try (ServerSocket serverSocket = new ServerSocket(5000)) {
                System.out.println("Server is listening on port 5000");
                Socket socket = serverSocket.accept();
                ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
                Person person = (Person) ois.readObject();
                System.out.println("Received Person: " + person.name + ", Age: " + person.age);
                ois.close();
                socket.close();
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }
        }).start();

        // Give the server a moment to start
        try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); }

        // Client code to send serialized object
        try (Socket socket = new Socket("localhost", 5000)) {
            ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
            Person person = new Person("Alice", 25);
            oos.writeObject(person);
            System.out.println("Sent Person: " + person.name + ", Age: " + person.age);
            oos.close();
        }
    }
}